package com.example.lxs_demo1.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import com.example.lxs_demo1.Api.ApiService;
import com.example.lxs_demo1.R;
import com.example.lxs_demo1.adapter.Fragment_Adapter;
import com.example.lxs_demo1.utils.SPUtil;

public class MainActivity extends FragmentActivity implements  RadioGroup.OnCheckedChangeListener, View.OnClickListener,ViewPager.OnPageChangeListener {
    private Fragment_Adapter fragementAdapter;
    private Button category;
    private Button settingbtn;
    private ViewPager layer_main;
    private RadioButton radio_chats;
    private RadioButton radio_contact;
    private RadioButton radio_me;
    private RadioGroup radioGroup_main;
    private LinearLayout homelayout;
    RadioGroup radioGroup;
    private ViewPager viewPager;
    EditText set_ip_et;
    EditText set_ip_port_et;
    String allbaseurl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initFragments();
    }

    private void initView() {
        category = (Button) findViewById(R.id.category);
        settingbtn = (Button) findViewById(R.id.settingbtn);
        layer_main = (ViewPager) findViewById(R.id.layer_main);
        radio_chats = (RadioButton) findViewById(R.id.radio_chats);
        radio_contact = (RadioButton) findViewById(R.id.radio_contact);
        radio_me = (RadioButton) findViewById(R.id.radio_me);
        radioGroup_main = (RadioGroup) findViewById(R.id.radioGroup_main);
        homelayout = (LinearLayout) findViewById(R.id.homelayout);

        category.setOnClickListener(this);
        settingbtn.setOnClickListener(this);
    }
    void initFragments() {
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup_main);
        radioGroup.setOnCheckedChangeListener(this);
        radioGroup.check(R.id.radio_chats);
        fragementAdapter = new Fragment_Adapter(getSupportFragmentManager());
        viewPager = (ViewPager) findViewById(R.id.layer_main);
        viewPager.setAdapter(fragementAdapter);
        viewPager.setCurrentItem(0);
        viewPager.setOnPageChangeListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.category:

                break;
            case R.id.settingbtn:
                setIp();
                break;
        }
    }

    private void setIp() {
        LayoutInflater layoutInflater=getLayoutInflater();
        View view=layoutInflater.inflate(R.layout.set_ip,null);
        set_ip_et = view.findViewById(R.id.et_set_ip);
        set_ip_port_et = view.findViewById(R.id.et_set_ip_port);
        set_ip_et.setText(SPUtil.getInstance(MainActivity.this).getSettingParam("set_ip", ApiService.SET_IP));
        set_ip_port_et.setText(SPUtil.getInstance(MainActivity.this).getSettingParam("set_ip_port", ApiService.SET_IP_POST));

        AlertDialog.Builder builder=new AlertDialog.Builder(this);
        builder.setView(view)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (set_ip_port_et.getText().toString().isEmpty() || set_ip_et.getText().toString().isEmpty()) {
                            showToast(MainActivity.this, "请输入IP和端口号");
                        } else {
                            allbaseurl = "http://" + set_ip_et.getText().toString() + ":" + set_ip_port_et.getText().toString();
                            SPUtil.getInstance(MainActivity.this).setSettingParam("set_ip", set_ip_et.getText().toString());
                            SPUtil.getInstance(MainActivity.this).setSettingParam("set_ip_port", set_ip_port_et.getText().toString());
                            SPUtil.getInstance(MainActivity.this).setSettingParam("baseurl", allbaseurl);
                            showToast(MainActivity.this, "设置成功！");
                            return;

                        }
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        return;
                    }
                }).show();
    }
    private static Toast toast;
    public static void showToast(Activity context, String content){
        if(toast==null){
            toast=Toast.makeText(context.getApplicationContext(),content,Toast.LENGTH_SHORT);
        }else{
            toast.setText(content);
        }
        toast.show();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        switch (checkedId) {
            case R.id.radio_chats:
                viewPager.setCurrentItem(0);
                break;
            case R.id.radio_contact:
                viewPager.setCurrentItem(1);
                break;
            case R.id.radio_me:
                viewPager.setCurrentItem(2);
                break;
            default:
                break;
        }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (state == 2) {
            //对应值需要与 FragmentPagerAdapter 中的对应
            switch (viewPager.getCurrentItem()) {
                case 0:
                    radio_chats.setChecked(true);
                    break;
                case 1:
                    radio_contact.setChecked(true);
                    break;
                case 2:
                    radio_me.setChecked(true);
                    break;
            }
        }
    }
}